package com.mazaiting.flutter

import android.os.Build
import android.os.Bundle
import android.widget.Toast

import io.flutter.app.FlutterActivity
import io.flutter.plugin.common.MethodChannel
import io.flutter.plugins.GeneratedPluginRegistrant

/**
 * Flutter 向 Native 主动调用 MethodChannel
 * Native 向 Flutter 发送事件 EventChannel
 */
class MainActivity: FlutterActivity() {
  companion object {
    /** Toast标识 */
    private const val TOAST = "com.mazaiting/toast"
  }
  
  override fun onCreate(savedInstanceState: Bundle?) {
    super.onCreate(savedInstanceState)
    //API>21,设置状态栏颜色透明
//    if(Build.VERSION.SDK_INT>=Build.VERSION_CODES.LOLLIPOP) {
//      window.statusBarColor = 0
//    }
    GeneratedPluginRegistrant.registerWith(this)
    
    initChannel()
  }
  
  /**
   * 初始化 Channel
   */
  private fun initChannel() {
    initToastChannel()
  }
  
  /**
   * Toast工具类
   */
  private fun initToastChannel() {
    // Toast 显示
    MethodChannel(flutterView, TOAST).setMethodCallHandler { methodCall, result ->
      println(methodCall.arguments)
      when(methodCall.method) {
        "showToast" -> {
          Toast.makeText(this, methodCall.argument<String>("msg"), Toast.LENGTH_SHORT).show()
          result.success("success")
        }
        else -> {
          // 未实现
          result.notImplemented()
        }
      }
    }
  }
}
